// ml:run = $bin < input
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct note{
    long long u, v;
    long long c;
    note() {}
    note (long long u, long long v, long long c):u(u), v(v), c(c){
    }
    bool operator<(const note &a)const{
        return c<a.c;
    }
}M[1000010];
const long long N = 300010;
long long n, m;
long long a[N], b[N], cost[N];
long long fa[N];
long long find(long long x){
    if (x==fa[x]) return x;
    fa[x]=find(fa[x]);
    return fa[x];
}
int main()
{
    scanf("%lld%lld", &n, &m);
    for (long long i=1;i<=n;i++) fa[i]=i;
    for (long long i=1;i<=n;i++){
        scanf("%lld%lld", &a[i], &b[i]);
        cost[i]=a[i]*b[i];
    }
    for (long long i=0;i<m;i++) scanf("%lld%lld%lld", &M[i].u, &M[i].v, &M[i].c);
    sort(M, M+m);
    for (long long i=0;i<m;i++){
        long long x=find(M[i].u), y=find(M[i].v);
        if (x==y) continue;
        long long A=max(a[x], max(a[y], M[i].c)), B=min(b[x], b[y]);
        cost[x]=min(cost[x]+cost[y], A*B);
        a[x]=A;
        b[x]=B;
        fa[y]=x;
    }
    long long ans=0;
    for (long long i=1;i<=n;i++)
        if (fa[i]==i)
            ans+=cost[i];
    printf("%lld\n", ans);
}

